from pydantic_settings import BaseSettings
import os


class Settings(BaseSettings):
    # 基础配置
    ENVIRONMENT: str = "development"
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    SERVER_PORT: int = 8187  # 实际服务端口，与main.py中的uvicorn端口一致
    DEBUG: bool = False
    
    # 数据库配置
    DB_HOST: str = "localhost"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = "password"
    DB_NAME: str = "livemgmt"
    
    # Redis配置
    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    REDIS_DB: int = 0
    
    # 文件存储配置
    RECORDING_PATH: str = "/data/recordings"
    THUMBNAIL_PATH: str = "/data/thumbnails"
    SEGMENT_PATH: str = "/data/segments"
    SCREENSHOT_PATH: str = "/data/screenshots"
    LOG_PATH: str = "/data/logs"
    
    # 日志配置
    LOG_LEVEL: str = "INFO"
    
    # FFmpeg配置
    FFMPEG_PATH: str = "ffmpeg"
    FFPROBE_PATH: str = "ffprobe"
    
    # HLS配置
    HLS_SEGMENT_DURATION: int = 60
    HLS_PLAYLIST_TYPE: str = "event"
    
    # 安全配置
    SECRET_KEY: str = "your-secret-key-here"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # CORS配置
    ALLOWED_HOSTS: list = [
        "*",
        "http://10.168.60.122:3000",
        "http://localhost:3000",
        "http://127.0.0.1:3000"
    ]
    
    # 监控配置
    MONITOR_INTERVAL: int = 30
    THUMBNAIL_INTERVAL: int = 60
    
    # 流监控配置
    STREAM_MONITOR_TIME: int = 60
    STREAM_MONITOR_MAX: int = 10
    FFMPEG_MAX_NUM: int = 20
    
    # 转推配置
    RTMP_PUSH_BASE_URL: str = "rtmp://localhost:1935/live"
    HLS_OUTPUT_BASE_URL: str = "http://localhost:8080/hls"
    
    # 兼容属性
    @property
    def DATABASE_URL(self) -> str:
        return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
    
    @property
    def REDIS_URL(self) -> str:
        return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"

    class Config:
        env_file = ".env"
        case_sensitive = True
        extra = "allow"  # 允许额外的环境变量


# 创建全局配置实例
settings = Settings()